package test;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import io.netty.handler.codec.TooLongFrameException;

import java.util.List;

/**
 * @author 张开顺
 * @title: FrameChunkDecoder
 * @projectName netty
 * @description: TODO
 * @date 2019/4/1014:13
 */
public class FrameChunkDecoder extends ByteToMessageDecoder {
    private final int maxFrameSize;
    public FrameChunkDecoder(int maxFrameSize){
        this.maxFrameSize=maxFrameSize;
    }

    @Override
    protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) throws Exception {
                int readalbebytes=byteBuf.readableBytes();
                if(readalbebytes>maxFrameSize){
                    byteBuf.clear();
                    throw new TooLongFrameException();
                }
                ByteBuf byteBuf1=byteBuf.readBytes(readalbebytes);
                list.add(byteBuf1);
    }
}
